﻿#pragma once
#ifndef WRBASICS_H
#define WRBASICS_H

#ifndef QT_NO_DEBUG
#define WRDEBUG
#endif

#ifndef WRDEBUG
#ifdef DEBUG
#define WRDEBUG
#endif
#endif

#include <cstddef>
#include <cstring>

typedef unsigned char WRByte;

typedef size_t WRu;
//typedef __ssize_t WRs;
typedef signed char WR8s;
typedef unsigned char WR8u;
typedef short WR16s;
typedef unsigned short WR16u;
typedef int WR32s;
typedef unsigned int WR32u;
typedef long long WR64s;
typedef unsigned long long WR64u;

typedef double WRf;
typedef float WR32f;
typedef double WR64f;

//定义只有一个字节的布尔类型。
class WRBool
{
private:
    char m_v;

public:
    inline WRBool(void)
    {
        m_v = 0;
    }
    inline WRBool(bool v)
    {
        m_v = v ? 1 : 0;
    }
    inline WRBool(const WRBool& v)
    {
        m_v = v.m_v;
    }
    inline WRBool& operator=(const bool& v)
    {
        m_v = v ? 1 : 0;
        return *this;
    }
    inline WRBool& operator=(const WRBool& v)
    {
        m_v = v.m_v;
        return *this;
    }
    inline operator bool(void) const
    {
        return m_v != 0;
    }
    inline bool operator==(bool v) const
    {
        if (v)
            return m_v != 0;
        return m_v == 0;
    }
    inline bool operator==(const WRBool& v) const
    {
        return m_v == v.m_v;
    }
    inline bool operator!=(bool v) const
    {
        if (v)
            return m_v == 0;
        return m_v != 0;
    }
    inline bool operator!=(const WRBool& v) const
    {
        return m_v != v.m_v;
    }
};

//typedef struct {
//    WRs  re;
//    WRs  im;
//} WRsc;
typedef struct {
    WRu  re;
    WRu  im;
} WRuc;
typedef struct {
    WR8s  re;
    WR8s  im;
} WR8sc;
typedef struct {
    WR8u  re;
    WR8u  im;
} WR8uc;
typedef struct {
    WR16s  re;
    WR16s  im;
} WR16sc;
typedef struct {
    WR16u  re;
    WR16u  im;
} WR16uc;
typedef struct {
    WR32s  re;
    WR32s  im;
} WR32sc;
typedef struct {
    WR32u  re;
    WR32u  im;
} WR32uc;
typedef struct {
    WR64s  re;
    WR64s  im;
} WR64sc;
typedef struct {
    WR64u  re;
    WR64u  im;
} WR64uc;

typedef struct {
    WRf  re;
    WRf  im;
} WRfc;
typedef struct {
    WR32f  re;
    WR32f  im;
} WR32fc;
typedef struct {
    WR64f  re;
    WR64f  im;
} WR64fc;

#define WRNull 0

#define WRENUM(name, type) \
private:\
    type m_value;\
public:\
    inline name(){}\
    inline name(const WR32s& value) : m_value((type)value){}\
    inline name(const type& value) : m_value(value){}\
    inline name(const name& other) : m_value(other.m_value){}\
    inline name& operator=(const WR32s& value){m_value = (type)value; return *this;}\
    inline name& operator=(const type& value){m_value = value; return *this;}\
    inline name& operator=(const name& other){m_value = other.m_value; return *this;}\
    inline bool operator==(const WR32s& value) const{return m_value == (type)value;}\
    inline bool operator==(const type& value) const{return m_value == value;}\
    inline bool operator==(const name& other) const{return m_value == other.m_value;}\
    inline bool operator!=(const WR32s& value) const{return m_value != (type)value;}\
    inline bool operator!=(const type& value) const{return m_value != value;}\
    inline bool operator!=(const name& other) const{return m_value != other.m_value;}\
    inline operator type() const{return m_value;}\
    inline operator WR32s() const{return (WR32s)m_value;}

#include <qstring.h>

#ifndef QStringLiteral
// no lambdas, not GCC, or GCC in C++98 mode with 4-byte wchar_t
// fallback, return a temporary QString
// source code is assumed to be encoded in UTF-8

# define QStringLiteral(str) QString::fromUtf8("" str "", sizeof(str) - 1)
#endif

#include <QtEndian>

#if Q_BYTE_ORDER == Q_BIG_ENDIAN
#define WRBIGENDIAN
#else
#define WRLITTLEENDIAN
#endif

#include <QUuid>

namespace WRBytes
{
    //从指定缓存中读取简单类型。
    template <class T> inline T read(const void* buf)
    {
        return qFromLittleEndian<T>((const uchar*)buf);
    }
    //将指定简单类型写入缓存中。
    template <class T> inline void write(T val, void* buf)
    {
        qToLittleEndian<T>(val, (uchar*)buf);
    }

    //从指定缓存中读取唯一标识。
    template <> inline QUuid read<QUuid>(const void* buf)
    {
        const unsigned char* p = (const unsigned char*)buf;
        return QUuid(
                    qFromLittleEndian<uint>(p),
                    qFromLittleEndian<ushort>(p + 4),
                    qFromLittleEndian<ushort>(p + 6),
                    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
    }
    //将指定唯一标识写入缓存中。
    template <> inline void write<QUuid>(QUuid val, void* buf)
    {
        unsigned char* p = (unsigned char*)buf;
        qToLittleEndian(val.data1, p);
        qToLittleEndian(val.data2, p + 4);
        qToLittleEndian(val.data3, p + 6);
        memcpy(p + 8, val.data4, 8);
    }
}

#endif // WRBASICS_H
